package com.wispx.gateway.core.helper;

import com.wispx.gateway.core.context.GatewayContext;
import com.wispx.gateway.core.response.GatewayResponse;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.*;
import org.asynchttpclient.Response;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * netty、网关、asyncHttpClient 响应之间的转换
 */
public class GatewayResponseHelper {

    private GatewayResponseHelper() {

    }


    /**
     * asyncHttpClient响应 -> GatewayResponse响应
     */
    public static GatewayResponse bulidGatewayResponse(Response response) {
        GatewayResponse gatewayResponse = new GatewayResponse();
        gatewayResponse.setResponseHeaders(response.getHeaders());
        gatewayResponse.setContent(response.getResponseBody());
        gatewayResponse.setResponseStatus(HttpResponseStatus.valueOf(response.getStatusCode()));
        gatewayResponse.setResponse(response);
        return gatewayResponse;
    }

    /**
     * GatewayResponse响应 -> Http响应
     */
    public static FullHttpResponse buildHttpResponse(GatewayContext context) {
        GatewayResponse gatewayResponse = context.getResponse();
        ByteBuf content;
        DefaultFullHttpResponse httpResponse;
        if (Objects.nonNull(gatewayResponse.getResponse()) || Objects.nonNull(gatewayResponse.getContent())) { // 下游有响应
            Response response = gatewayResponse.getResponse();
            // 下游响应 || 下游响应内容不为null
            if (Objects.nonNull(gatewayResponse.getResponse())) {
                content = Unpooled.wrappedBuffer(response.getResponseBodyAsByteBuffer());
            } else {
                content = Unpooled.wrappedBuffer(gatewayResponse.getContent().getBytes(StandardCharsets.UTF_8));
            }
            httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.valueOf(response.getStatusCode()), content);
            httpResponse.headers().add(response.getHeaders());
        } else {// 下游响应为null
            content = Unpooled.wrappedBuffer("non response".getBytes(StandardCharsets.UTF_8));
            httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    gatewayResponse.getResponseStatus(), content);
            httpResponse.headers().add(gatewayResponse.getResponse().getHeaders());
            httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpResponse.content().readableBytes());// Content-Length，值为响应内容的字节数
        }
        return httpResponse;
    }

    public static GatewayResponse bulidGatewayResponse() {
        GatewayResponse gatewayResponse = new GatewayResponse();
        // TODO 封装响应码
        gatewayResponse.addHeader(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
        gatewayResponse.setResponseStatus(HttpResponseStatus.ACCEPTED);
        gatewayResponse.setContent("OK");
        return gatewayResponse;
    }

    /**
     * 根据响应码构建http响应
     */
    public static FullHttpResponse buildHttpResponse(HttpResponseStatus responseCode) {
        DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                responseCode);
        httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON + ";charset=utf-8");
        httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpResponse.content().readableBytes());

        return httpResponse;
    }

}
